package com.sl.plat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.water.entity.BRiskTip;
import com.sl.water.service.BRiskTipService;
import com.sl.water.vo.BRiskTipIVO;
import com.sl.water.vo.BaseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plat/risk/tip")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预警-风险提示单", description = "提供风险提示单增、删、改、查、列表查询相关功能")
public class BRiskTipController {

    private final BRiskTipService bRiskTipService;

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<BRiskTip>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size) {
        Page<BRiskTip> bRiskTipPage = bRiskTipService.pageList(page, size);
        return ResponseData.success(bRiskTipPage);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public ResponseData<List<BRiskTip>> getList() {
        return ResponseData.success(bRiskTipService.list());
    }

    @PostMapping
    @Operation(summary = "新增")
    public ResponseData save(@RequestBody BRiskTipIVO bRiskTipVO) {
        BRiskTip bRiskTip = BaseVO.convert(bRiskTipVO, BRiskTip.class);
        bRiskTip.setState("未发送");
        bRiskTipService.save(bRiskTip);
        return ResponseData.success(bRiskTip.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        bRiskTipService.removeById(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    public ResponseData updateById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @RequestBody BRiskTipIVO bRiskTipVO) {
        BRiskTip bRiskTip = BaseVO.convert(bRiskTipVO, BRiskTip.class);
        bRiskTip.setId(id);
        bRiskTipService.updateById(bRiskTip);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询")
    public ResponseData<BRiskTip> getById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(bRiskTipService.getById(id));
    }

    @PutMapping("/updateState/{id}")
    @Operation(summary = "修改状态")
    public ResponseData updateState(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @Parameter(
                    description = "状态(未发送、已发送)",
                    required = true,
                    example = "已发送"
            )
            @RequestParam String state) {
        bRiskTipService.updateState(id, state);
        return ResponseData.success("修改成功");
    }

}